<?php

namespace App\Http\Controllers\main;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class MainController extends Controller
{
   /**
     * Info()    从数据库中获取数据的静态方法
     *
     * @return $data array 返回商品信息数组
     */
    static public function GoodsInfo()
    {
        
        // 查询商品配置表
        $data = DB::table('goods_config')
        // 关联商品表
        -> join('goods','goods.id','=','goods_config.goods_id')
        // 关联查询商品详细配置表
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        // 关联查询商品其他配置表
        -> join('goods_config_other','goods_config_other.id','=','goods_config.goods_config_other')
        // 关联查询商品图片
        -> join('goods_datalist_pic','goods_datalist_pic.id','=','goods_config.goods_datalist_pic')
        -> join('goods_cate','goods_cate.id','=','goods.goods_sort')
        // -> select('goods.*','goods_config.*','goods_config_list.*')
        -> get();
        // 返回数据数组
        return $data;
    }

    /**
     * GoodsConfigCate() 获取商品配置分类名称并用|----拼接好并进行分类排序
     * 
     * @param   $length int 分类路径中逗号的数量
     * @param   $data array 分类数组
     * 
     * @return  返回拼接好的分类名称数组
     */
    static public function GoodsConfigCate()
    {
        // 查询数据库中的分类名称,并根据分类排序
        $data = DB::table('goods_config_cate') -> select(DB::raw("*,concat(goods_config_path,',',id) as paths"))-> orderBy('paths') -> get();
        // 遍历数组进行查询数据库中数据
        foreach($data as $key=>$value){
            // 根据分类路径中的逗号数量
            $length = substr_count($value['goods_config_path'],',');
            // 根据逗号数量进行分类名的拼接,通过|----来拼接显示级别
            $data[$key]['goods_config_cate'] = str_repeat('|----',$length).$value['goods_config_cate'];
        }
        return $data;
    }

    /**
     * getInfo() 获取商品分类名称并用|----拼接好并进行分类排序
     * 
     * @param   $length int 分类路径中逗号的数量
     * @param   $data array 分类数组
     * 
     * @return  返回拼接好的分类名称数组
     */
    static public function CateInfo()
    {
        // 查询数据库中的分类名称,并根据分类排序
        $data = DB::table('goods_cate') -> select(DB::raw("*,concat(goods_path,',',id) as paths"))-> orderBy('paths') -> get();
        // 遍历数组进行查询数据库中数据
        foreach($data as $key=>$value){
            // 根据分类路径中的逗号数量
            $length = substr_count($value['goods_path'],',');
            // 根据逗号数量进行分类名的拼接,通过|----来拼接显示级别
            $data[$key]['goods_name'] = str_repeat('|----',$length).$value['goods_name'];
        }

        return $data;
    } 

    /**
     * getDatalist()   根据id获取需要查找的商品信息
     * 
     * @param  string $id 必须传入一个id值
     * 
     * @return $data  array  返回商品信息数组
     */
    static public function getDatalist($id)
    {
        // 查询商品配置表
        $data = DB::table('goods_config')
        // 关联商品表
        -> join('goods','goods.id','=','goods_config.goods_id')
        // 关联查询商品详细配置表
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        -> join('goods_cate','goods_cate.id','=','goods.goods_sort')
        // 关联查询商品其他配置表
        -> join('goods_config_other','goods_config_other.id','=','goods_config.goods_config_other')
        // 关联查询商品图片
        -> join('goods_datalist_pic','goods_datalist_pic.id','=','goods_config.goods_datalist_pic')
        
        -> join('goods_datalist','goods_datalist.id','=','goods_config.goods_datalist')
        -> join('goods_inventory','goods_inventory.goods_config','=','goods_config.id')
        // -> select('goods.*','goods_config.*','goods_config_list.*')
        -> where('goods.id','=',$id)
        -> select('goods_inventory.goods_inventory','goods_inventory.id as inventory_id','goods.*','goods_config_list.*','goods_config_other.*','goods_datalist_pic.*','goods_cate.*','goods_datalist.*','goods_config.id as id','goods_config.*' )
        -> first();
        // dd($data);
        return $data;
    }

    /**
     * Goodsson()    
     * @param [type] $id [description]
     */
    static public function Goodsson($id)
    {
        // 查询商品子类
        $data = DB::table('goods_config')
        -> join('goods','goods.id','=','goods_config.goods_id')
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        -> join('goods_inventory','goods_inventory.goods_config','=','goods_config.id')
        -> where('goods.id','=',$id)
        -> select('goods_inventory.goods_inventory','goods_inventory.id as inventory_id','goods.*','goods_config_list.*','goods_config.id as id','goods_config.*' )
        -> get();
        // dd($data);
        return $data;
    }

    /**
     * SearchGoods()  搜索方法查询
     *
     * @return arr    返回查询数组
     */
    static public function SearchGoods($search,$desc,$order)
    {

       $data = DB::table('goods_config')
        -> join('goods','goods.id','=','goods_config.goods_id')
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        -> join('goods_datalist_pic','goods_datalist_pic.id','=','goods_config.goods_datalist_pic')
        -> join('goods_cate','goods_cate.id','=','goods.goods_sort')
        -> where('goods.goods_model','like','%'.$search.'%')
        -> orWhere('goods_cate.goods_name','like','%'.$search.'%')
        -> orWhere('goods_config_list.goods_config_cpu_brand','like','%'.$search.'%')
        -> orWhere('goods_config_list.goods_config_cpu_core','like','%'.$search.'%')
        -> orWhere('goods_config_list.goods_config_cpu_type','like','%'.$search.'%')
        -> orWhere('goods_config_list.goods_config_cpu_type','like','%'.$search.'%')
        -> orWhere('goods.goods_title','like','%'.$search.'%')
        -> select('goods_datalist_pic.goods_datalist_pic1','goods_config.id','goods_config.goods_id','goods_model','goods.goods_sort','goods.goods_title','goods.goods_sales','goods_art','goods_stick','goods_boutique','goods_status','goods_price','goods_config_list.goods_config_cpu_brand','goods_config_list.goods_config_cpu_core','goods_config_list.goods_config_cpu_clock','goods_config_list.goods_config_cpu_type','goods_config_list.goods_config_cpu_ddr','goods_config_list.goods_config_cpu_series','goods_config_list.goods_config_disk_brand','goods_config_list.goods_config_disk_kind','goods_config_list.goods_config_disk_size','goods_config_list.goods_config_gpu_brand','goods_config_list.goods_config_gpu_type','goods_config_list.goods_config_gpu_ddr','goods_config_list.goods_config_gpu_size','goods_config_list.goods_config_mainborad_brand','goods_config_list.goods_config_mainborad_type','goods_config_list.goods_config_ram_brand','goods_config_list.goods_config_ram_ddr','goods_config_list.goods_config_ram_size')
        -> orderBy($desc,$order)
        -> get();

        // dd($data);
        return $data;
    }

    /**
     * 上传图片
     *
     * @param       int      $w          获取上传图片的宽度
     * @param       int      $h          获取上传图片的高度
     * @param       string   $path       设定上传图片的路径
     * @param       string   $temp_name  随机设定的文件名称
     */
    static public function upload(Request $request,$w,$h)
    {
        
        // 判断文件是否上传
        if($request -> hasFile('pic')){
            $file = $request -> file('pic');


            // 获取图片的基本信息 长与宽
            $info = getimageSize($file);
            // 获取宽度
            $width = $info[0];  
            // 获取高度
            $height = $info[1];
            if($width != $w){
                return false;
            }
            if($height != $h){
                return false;
            }
            
        }
        return $temp_name;
    }

}
